package com.yupi.yupibackend.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.yupibackend.annotation.AuthCheck;
import com.yupi.yupibackend.common.BaseResponse;
import com.yupi.yupibackend.common.DeleteRequest;
import com.yupi.yupibackend.common.ResultUtils;
import com.yupi.yupibackend.constants.UserConstant;
import com.yupi.yupibackend.exception.ErrorCode;
import com.yupi.yupibackend.exception.ThrowUtils;
import com.yupi.yupibackend.model.entity.InterfaceInfo;
import com.yupi.yupibackend.model.entity.User;
import com.yupi.yupibackend.model.req.interfaceInfo.InterfaceInfoAddRequest;
import com.yupi.yupibackend.model.req.interfaceInfo.InterfaceInfoQueryRequest;
import com.yupi.yupibackend.model.req.interfaceInfo.InterfaceInfoUpdateRequest;
import com.yupi.yupibackend.model.vo.InterfaceInfoVo;
import com.yupi.yupibackend.service.InterfaceInfoService;
import com.yupi.yupibackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 小新
 * @Date: 2025/5/4 11:36
 * @Description:
 * @Version: v1.0
 */
@RestController
@RequestMapping("/interfaceInfo")
@Slf4j
public class InterfaceInfoController {

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @Resource
    private UserService userService;

    /**
     * 添加接口信息
     *
     * @param interfaceInfoAddRequest
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/add")
    public BaseResponse<Long> addInterfaceInfo(@RequestBody InterfaceInfoAddRequest interfaceInfoAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(interfaceInfoAddRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Long interfaceInfoId = interfaceInfoService.addInterfaceInfo(interfaceInfoAddRequest,loginUser);
        return ResultUtils.success(interfaceInfoId);
    }

    /**
     * 更新接口信息
     *
     * @param interfaceInfoUpdateRequest
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/update")
    public BaseResponse<Boolean> updateInterfaceInfo(@RequestBody InterfaceInfoUpdateRequest interfaceInfoUpdateRequest) {
        ThrowUtils.throwIf(interfaceInfoUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        boolean result = interfaceInfoService.updateInterfaceInfo(interfaceInfoUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 删除接口信息
     * @param deleteRequest
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteInterfaceInfo(@RequestBody DeleteRequest deleteRequest) {
        ThrowUtils.throwIf(deleteRequest == null, ErrorCode.PARAMS_ERROR);
        boolean result = interfaceInfoService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * 查询接口信息
     * @param interfaceInfoQueryRequest
     * @return
     */
    @PostMapping("/query")
    public BaseResponse<InterfaceInfoVo> queryInterfaceInfo(@RequestBody InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        ThrowUtils.throwIf(interfaceInfoQueryRequest == null, ErrorCode.PARAMS_ERROR);
        InterfaceInfoVo interfaceInfoVo = interfaceInfoService.queryInterfaceInfo(interfaceInfoQueryRequest);
        return ResultUtils.success(interfaceInfoVo);
    }

    /**
     * 分页获取接口信息封装列表
     * @param interfaceInfoQueryRequest 查询请求参数
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<InterfaceInfoVo>> listInterfaceVOByPage(@RequestBody InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        ThrowUtils.throwIf(interfaceInfoQueryRequest == null, ErrorCode.PARAMS_ERROR);
        long current = interfaceInfoQueryRequest.getCurrent();
        long pageSize = interfaceInfoQueryRequest.getPageSize();
        Page<InterfaceInfo> InterfaceInfoPage = interfaceInfoService.page(new Page<>(current, pageSize), interfaceInfoService.getQueryWrapper(interfaceInfoQueryRequest));
        Page<InterfaceInfoVo> InterfaceInfoVOPage = new Page<>(current, pageSize, InterfaceInfoPage.getTotal());
        List<InterfaceInfoVo> interfaceInfoVoList = interfaceInfoService.getInterfaceInfoVoList(InterfaceInfoPage.getRecords());
        InterfaceInfoVOPage.setRecords(interfaceInfoVoList);
        return ResultUtils.success(InterfaceInfoVOPage);
    }

}


